#include "ControllerInput.h"

//-------------------------------------------------------------------------------------
ControllerInput::ControllerInput()
{ }

//-------------------------------------------------------------------------------------
ControllerInput::~ControllerInput()
{ }

//-------------------------------------------------------------------------------------
ControllerInput::ControllerInput(const ControllerInput& rhs)
{ }

//-------------------------------------------------------------------------------------
bool ControllerInput::Initialise()
{
	// Attempt to get the state of the Xbox 360 controller that is connected to the first port.
	unsigned long result = XInputGetState(0, &m_controller_state);

	// Output an error message if the controller failed to initialise.
	if (result != ERROR_SUCCESS)
	{
		MessageBox(NULL, L"Failed to Initialise Controller.", L"Input Error", MB_OK);

		return false;
	}

	return true;
}

//-------------------------------------------------------------------------------------
void ControllerInput::Update()
{
	// Update the state of the Xbox 360 controller that is connected to the first port.
	unsigned long result = XInputGetState(0, &m_controller_state);

	// Output an error message if the controller failed to update.
	if (result != ERROR_SUCCESS)
	{
		MessageBox(NULL, L"Controller Failed to Update!", L"Input Error", MB_OK);
	}
}

//-------------------------------------------------------------------------------------
float ControllerInput::get_thumbLX() const
{
	if (m_controller_state.Gamepad.sThumbLX	< -5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbLX * 0.00005);

		return magnitude;
	}

	else if (m_controller_state.Gamepad.sThumbLX > 5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbLX * 0.00005);

		return magnitude;
	}

	return 0.0f;
}

//-------------------------------------------------------------------------------------
float ControllerInput::get_thumbLY() const
{
	if (m_controller_state.Gamepad.sThumbLY < -5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbLY * 0.00005);

		return magnitude;
	}

	else if (m_controller_state.Gamepad.sThumbLY > 5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbLY * 0.00005);

		return magnitude;
	}

	return 0.0f;
}

//-------------------------------------------------------------------------------------
float ControllerInput::get_thumbRX() const
{
	if (m_controller_state.Gamepad.sThumbRX < -5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbRX * 0.00008);

		magnitude *= -1;

		return magnitude;
	}

	else if (m_controller_state.Gamepad.sThumbRX > 5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbRX * 0.00008);

		magnitude *= -1;

		return magnitude;
	}

	return 0.0f;
}

//-------------------------------------------------------------------------------------
float ControllerInput::get_thumbRY() const
{
	if (m_controller_state.Gamepad.sThumbRY < -5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbRY * 0.00008);

		return magnitude;
	}

	else if (m_controller_state.Gamepad.sThumbRY > 5000)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.sThumbRY * 0.00008);

		return magnitude;
	}

	return 0.0f;
}

//-------------------------------------------------------------------------------------
float ControllerInput::get_leftTrigger() const
{
	if (m_controller_state.Gamepad.bLeftTrigger > 50)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.bLeftTrigger * 0.02f);

		return magnitude;
	}

	return 0.0f;
}

//-------------------------------------------------------------------------------------
float ControllerInput::get_rightTrigger() const
{
	if (m_controller_state.Gamepad.bRightTrigger > 50)
	{
		float magnitude = static_cast<float>(m_controller_state.Gamepad.bRightTrigger * 0.02f);

		magnitude *= -1;	// For dollying in.

		return magnitude;
	}

	return 0.0f;
}

//-------------------------------------------------------------------------------------
bool ControllerInput::get_leftShoulder() const		// Get left shoulder button.
{
	if (m_controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER)
	{
		return true;
	}

	return false;
}

//-------------------------------------------------------------------------------------
bool ControllerInput::get_rightShoulder() const		// Get right shoulder button.
{
	if (m_controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER)
	{
		return true;
	}

	return false;
}

//-------------------------------------------------------------------------------------
bool ControllerInput::get_buttonA() const
{
	if (m_controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_A)
	{
		return true;
	}

	return false;
}

//-------------------------------------------------------------------------------------
bool ControllerInput::get_buttonX() const
{
	if (m_controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_X)
	{
		return true;
	}

	return false;
}